home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 13 / Example 13.2 / mouse.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-16  |  3.1 KB  |  132 lines

  1. #include "mouse.h"
  2.  
  3. MOUSE::MOUSE()
  4. {
  5.     m_type = 0;
  6.     m_pMouseTexture = NULL;
  7.     m_pMouseDevice = NULL;
  8.     m_speed = 1.5f;
  9.     m_inputBlock = GetTickCount();
  10. }
  11.  
  12. MOUSE::~MOUSE()
  13. {
  14.     if(m_pMouseDevice != NULL)
  15.         m_pMouseDevice->Release();
  16.  
  17.     if(m_pMouseTexture != NULL)
  18.         m_pMouseTexture->Release();
  19. }
  20.  
  21. void MOUSE::InitMouse(IDirect3DDevice9* Device, HWND wnd)
  22. {
  23.     try
  24.     {
  25.         //Load texture and init sprite
  26.         D3DXCreateTextureFromFile(Device, "cursor/cursor.dds", &m_pMouseTexture);
  27.         D3DXCreateSprite(Device, &m_pSprite);
  28.  
  29.         //Get directinput object
  30.         LPDIRECTINPUT8 directInput = NULL;
  31.  
  32.         DirectInput8Create(GetModuleHandle(NULL),    // Retrieves the application handle
  33.                            0x0800,                    // v.8.0    
  34.                            IID_IDirectInput8,        // Interface ID
  35.                            (VOID**)&directInput,    // Our DirectInput Object
  36.                            NULL);
  37.  
  38.         //Acquire Default System mouse
  39.         directInput->CreateDevice(GUID_SysMouse, &m_pMouseDevice, NULL);        
  40.         m_pMouseDevice->SetDataFormat(&c_dfDIMouse);
  41.         m_pMouseDevice->SetCooperativeLevel(wnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
  42.         m_pMouseDevice->Acquire();            
  43.  
  44.         //Get viewport size and init mouse location
  45.         D3DVIEWPORT9 v;
  46.         Device->GetViewport(&v);
  47.         m_viewport.left = v.X;
  48.         m_viewport.top = v.Y;
  49.         m_viewport.right = v.X + v.Width;
  50.         m_viewport.bottom = v.Y + v.Height;
  51.  
  52.         x = v.X + v.Width / 2.0f;
  53.         y = v.Y + v.Height / 2.0f;
  54.  
  55.         //Release Direct Input Object
  56.         directInput->Release();
  57.     }
  58.     catch(...)
  59.     {
  60.         debug.Print("Error in MOUSE::InitMouse()");
  61.     }    
  62. }
  63.  
  64. bool MOUSE::ClickLeft()
  65. {
  66.     if(GetTickCount() < m_inputBlock)return false;
  67.     return m_mouseState.rgbButtons[0];
  68. }
  69.  
  70. bool MOUSE::ClickRight()
  71. {
  72.     if(GetTickCount() < m_inputBlock)return false;
  73.     return m_mouseState.rgbButtons[1];
  74. }
  75.  
  76. bool MOUSE::WheelUp()
  77. {
  78.     if(GetTickCount() < m_inputBlock)return false;
  79.     return m_mouseState.lZ > 0.0f;
  80. }
  81.  
  82. bool MOUSE::WheelDown()
  83. {
  84.     if(GetTickCount() < m_inputBlock)return false;
  85.     return m_mouseState.lZ < 0.0f;
  86. }
  87.  
  88. bool MOUSE::Over(RECT dest)
  89. {
  90.     if(x < dest.left || x > dest.right)return false;
  91.     if(y < dest.top || y > dest.bottom)return false;
  92.     return true;
  93. }
  94.  
  95. bool MOUSE::PressInRect(RECT dest)
  96. {
  97.     return ClickLeft() && Over(dest);
  98. }
  99.  
  100. void MOUSE::Update()
  101. {
  102.     //Retrieve mouse state
  103.     ZeroMemory(&m_mouseState, sizeof(DIMOUSESTATE));
  104.     m_pMouseDevice->GetDeviceState(sizeof(DIMOUSESTATE), &m_mouseState);
  105.  
  106.     //Update pointer
  107.     x += m_mouseState.lX * m_speed;
  108.     y += m_mouseState.lY * m_speed;
  109.  
  110.     //Keep mouse pointer within window
  111.     if(x < m_viewport.left)    x = m_viewport.left;
  112.     if(y < m_viewport.top)    y = m_viewport.top;
  113.     if(x > m_viewport.right)    x = m_viewport.right;
  114.     if(y > m_viewport.bottom)    y = m_viewport.bottom;
  115. }
  116.  
  117. void MOUSE::Paint()
  118. {    
  119.     if(m_pMouseTexture != NULL)
  120.     {
  121.         RECT src[] = {{0,0,20,20}, {0,20,20,40}, {20,20,40,40}, {0,40,20,60}, {20,40,40,60}};
  122.  
  123.         m_pSprite->Begin(D3DXSPRITE_ALPHABLEND);
  124.         m_pSprite->Draw(m_pMouseTexture, &src[m_type], NULL, &D3DXVECTOR3(x, y, 0.0f), 0xffffffff);
  125.         m_pSprite->End();
  126.     }    
  127. }
  128.  
  129. void MOUSE::DisableInput(int millisec)
  130. {
  131.     m_inputBlock = GetTickCount() + millisec;
  132. }